//
//  MYGHeadDetailFooterView.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/7.
//

import UIKit
import QMUIKit

class MYGHeadDetailFooterView: UIView {
    var remarkAction: (() -> Void)?
    var shareAction: (() -> Void)?
    var likeAction: (() -> Void)?
    var starAction: (() -> Void)?
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - UI
    private func configUI() {
        backgroundColor = .white
        addSubview(enterView)
        addSubview(stackView)
        addSubview(lineView)
        
        stackView.addArrangedSubviews([shareButton, likeButton, starButton])
        enterView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.top.equalToSuperview().offset(11)
        }
        
        stackView.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(14)
            make.centerY.equalTo(enterView)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    // MARK: - lazy load
    private lazy var enterView: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("写评论", for: .normal)
        btn.setTitleColor(.rgba(51, 51, 51, 1), for: .normal)
        btn.titleLabel?.font = .regular(13)
        btn.setImage(UIImage(named: "edit"), for: .normal)
        btn.spacingBetweenImageAndTitle = 9
        btn.imagePosition = .left
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.spacing = 20
        stackV.axis = .horizontal
        return stackV
    }()
    
    private(set) lazy var shareButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "share"), for: .normal)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    private(set) lazy var likeButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "great"), for: .normal)
        btn.setImage(UIImage(named: "great_sel"), for: .selected)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)

        return btn
    }()
    
    private(set) lazy var starButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "star"), for: .normal)
        btn.setImage(UIImage(named: "star_sel"), for: .selected)
        btn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        return btn
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(238, 238, 238, 1)
        return view
    }()
}

extension MYGHeadDetailFooterView {
    @objc
    private func clickButton(_ btn: UIButton) {
        if btn == enterView {
            remarkAction?()
        } else if btn == shareButton {
            shareAction?()
        } else if btn == likeButton {
            likeAction?()
        } else if btn == starButton {
            starAction?()
        }
    }
}
